import * as MutationTypes from './mutation-types';

const mutations = {
  [MutationTypes.ADD_INCLUDE] (state, data) {
    const { includes } = state;
    if (includes.includes(data)) {
      return;
    }

    includes.push(data);
  },

  [MutationTypes.REMOVE_INCLUDE] (state, data) {
    const { includes } = state;
    const idx = includes.findIndex(item => item === data);
    if (idx === -1) {
      return;
    }

    includes.splice(idx, 1);
  },

  [MutationTypes.ADD_INCLUDES] (state, data) {
    if (!Array.isArray(data)) {
      return;
    }

    data.forEach(item => {
      this.$store.commit(MutationTypes.ADD_INCLUDE, item);
    });
  },

  [MutationTypes.REMOVE_INCLUDES] (state, data) {
    if (!Array.isArray(data)) {
      return;
    }

    data.forEach(item => {
      this.$store.commit(MutationTypes.REMOVE_INCLUDE, item);
    });
  },

  [MutationTypes.SET_INCLUDES] (state, data) {
    if (!Array.isArray(data)) {
      return;
    }

    state.includes = data;
  }
};

export default mutations;
